package com.wq.jdk8;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Author: wangqiang20995
 * @Date:2019/6/20
 * @Description:
 * @Resource:
 */
public class RegexDemo {

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("1", "one");
        map.put("2", "two");
        map.put("password", "12345q6");
        map.put("4", "four");

        String plain = map.toString();
        String json = JSONObject.toJSONString(map);
        String xml = "<root><1>one</1><2>two</2><PASSWORD>123456</password><4>four</4>";
        String xmlLine = "<root>\n" +
                "\t<1>one</1>\n" +
                "\t<2>two</2>\n" +
                "\t<password>123456</password>\n" +
                "\t<4>four</4>\n" +
                "</root>";

//        String regex = "[\"<]?(password)[\">]?[[=:]|\n?][\"(</)]?[\\w]+[\">]?";
//        String regex = "[\"<]?(password)[\">]?[[=:]|\n*\t*][\"(</)]?[\\w]+[\">]?";
        String regex = "\"?((?i)PASSWORD)\"?[=:]\"?\\w+\"?";
        System.out.println(plain.replaceAll(regex, "password=******"));
        System.out.println(json.replaceAll(regex, "\"password\":\"******\""));

//        System.out.println("<password>123456</password>".replaceAll("[\"<]?password[\">]?[[=:]|[\n\t*]]*\"?\\w+\"?(</password>)", "<******>"));
//        System.out.println(xml.replaceAll("[\"<]?password[\">]?[[=:]|[\n\t*]]*\"?\\w+\"?(</password>)", "<password>******</password>"));
        String s = "<(?i)password>?[[=:]|[\n\t*]]*\\w+</(?i)password>";
        System.out.println(xml.replaceAll("<(?i)PASSWORD>?[[=:]|[\n\t*]]*\\w+</(?i)PASSWORD>", "<password>******</password>"));
        System.out.println(xmlLine.replaceAll("<(?i)PASSWORD>[[=:]|[\n\t*]]*\\w+</(?i)Password>", "<password>******</password>"));
        System.out.println(xml.replaceAll(s,"<password>******</password>"));

        System.out.println(Pattern.compile("<\\w+>\\w+(\n*\t*)</\\w+>").matcher(xml).matches());

        System.out.println(Pattern.compile("[\\s\\S]+<(?i)password>[\\s\\S]+").matcher(xmlLine).matches());

        String m = "{1=one, 2=two, www=wer, PassWord=1234abc}";
//        String p = ".+\"?(?i)password\"[\t\r\n]?=.+";
        String p = ".+\\s+\"?(?i)password\"?\\s*=\"?\\w+\"?.+";
        System.out.println(Pattern.compile(p).matcher(m).matches());


    }
}
